package model.hospital;

import net.passClass.PassData;

import java.util.ArrayList;
import java.util.Objects;

public class Bed implements Comparable<Bed>{
    private int bedNumber;
    private String state;
    private String uid;
    private String patientUID;

    public PassData check_in(Patient patient) {
        if(patientUID!=null){
            return new PassData(false,null,"床位已有病人，签入失败");
        }
        patientUID = patient.getUid();
        return new PassData(true,null,"签入成功");
    }
    public PassData check_out() {
        if(patientUID==null){
            return new PassData(false,null,"床位没有病人，签出失败");
        }
        ArrayList<Patient> patients = PatientDB.getInstance().get(new Patient(patientUID)).getData();
        if(patients==null){
            return new PassData(false,null,"Fatal Error:病人不存在，签出失败");
        }
        Patient aPatient = patients.get(0);
        System.out.println(aPatient);

        aPatient.setBedUID(null);
        patientUID = null;
        return new PassData(true,null,"签出成功");
    }
    public PassData exchange(Bed otherBed){
        String patientA = this.patientUID;
        String patientB = otherBed.patientUID;
        if(patientA!=null){
            ArrayList<Patient> patients = PatientDB.getInstance().get(new Patient(patientA)).getData();
            patients.get(0).setBedUID(otherBed.uid);
        }
        if(patientB!=null){
            ArrayList<Patient> patients = PatientDB.getInstance().get(new Patient(patientB)).getData();
            patients.get(0).setBedUID(this.uid);
        }
        this.patientUID = patientB;
        otherBed.patientUID = patientA;
        return new PassData(true,null,"交换成功");
    }

    public Bed() {
    }

    public Bed(int bedNumber, String state, String uid, String patientUID) {
        this.bedNumber = bedNumber;
        this.state = state;
        this.uid = uid;
        this.patientUID = patientUID;
    }

    public Bed(int bedNumber) {
        this.bedNumber = bedNumber;
    }

    @Override
    public int compareTo(Bed o) {
        return this.bedNumber - o.bedNumber;
    }

    public int getBedNumber() {
        return bedNumber;
    }

    public void setBedNumber(int bedNumber) {
        this.bedNumber = bedNumber;
    }

    public String getState() {
        return state;
    }

    public void setState(String state) {
        this.state = state;
    }

    public String getUid() {
        return uid;
    }

    public void setUid(String uid) {
        this.uid = uid;
    }

    public String getPatientUID() {
        return patientUID;
    }

    public void setPatientUID(String patientUID) {
        this.patientUID = patientUID;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Bed bed = (Bed) o;
        return Objects.equals(uid, bed.uid);
    }

    @Override
    public String toString() {
        return "Bed{" +
                "bedNumber=" + bedNumber +
                ", state='" + state + '\'' +
                ", uid='" + uid + '\'' +
                ", patientUID='" + patientUID + '\'' +
                '}';
    }
}
